package com.artpri.dao.impl;

import java.math.BigInteger;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import org.hibernate.Query;
import org.hibernate.SQLQuery;
import org.hibernate.transform.Transformers;

import com.artpri.dao.SimpleGoodsDao;
import com.artpri.model.vo.SimpleGoods;

/**
 * @author ZhangSi
 */
public class SimpleGoodsDaoImpl extends HibernateDaoImpl<SimpleGoods> implements SimpleGoodsDao {

	@SuppressWarnings("unchecked")
	@Override
	public List<SimpleGoods> find(String sql, Object... params) {
		Query query = getSessionFactory().getCurrentSession()
			.createSQLQuery(sql)
			.setResultTransformer(Transformers.aliasToBean(SimpleGoods.class));
		
		for(int i = 0 , len = params.length ; i < len ; i++)
		{
			query.setParameter(i + "" , params[i]);
		}
		
		return (List<SimpleGoods>)query.list();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<SimpleGoods> findByPage(String sql, int pageNo, int pageSize) {
		return getSqlQuery(sql).setResultTransformer(Transformers.aliasToBean(SimpleGoods.class))
				.setFirstResult((pageNo -1) * pageSize)
				.setMaxResults(pageSize)
				.list();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<SimpleGoods> findByPage(String sql, int pageNo, int pageSize, Object... params) {
		// 创建查询
		SQLQuery query = getSessionFactory().getCurrentSession().createSQLQuery(sql);
		// 为包含占位符的HQL语句设置参数
		for(int i = 0 , len = params.length ; i < len ; i++)
		{
			query.setParameter(i + "" , params[i]);
		}
		// 执行分页，并返回查询结果
		return query.setResultTransformer(Transformers.aliasToBean(SimpleGoods.class))
			.setFirstResult((pageNo - 1) * pageSize)
			.setMaxResults(pageSize)
			.list();
	}

	@Override
	public Integer findCount(String sql) {
		BigInteger bin = (BigInteger) getSqlQuery(sql).uniqueResult();
		return (Integer) bin.intValue();
	}

	@Override
	public SimpleGoods get(Integer goodsNum) {
		String sql = "select g.user_num userNum, u.user_name userName, g.goods_num goodsNum, g.goods_name goodsName, g.goods_price goodsPrice, g.goods_leave goodsLeave, g.goods_add_time goodsAddTime, a.ga_id gaId, a.ga_path mainpic"
				+ " from goods g, goods_album a, user_info u "
				+ " where g.goods_num=" + goodsNum + " and a.goods_num=g.goods_num and g.user_num=u.user_num and a.ga_main=1";
		return (SimpleGoods) getSqlQuery(sql).setResultTransformer(Transformers.aliasToBean(SimpleGoods.class))
				.uniqueResult();
	}
	
	@Override
	public List<SimpleGoods> get(List<Integer> goodsNums) {
		List<SimpleGoods> list = new ArrayList<>();
		for (Iterator<Integer> it = goodsNums.iterator(); it.hasNext();) {
			SimpleGoods sg = get((Integer) it.next());
			list.add(sg);
		}
		return list;
	}
	
	@Override
	public SimpleGoods findSimpleGoodsToBuyWhat(int goodsNum) {

		String sql = "select g.goods_num  goodsNum, g.goods_name goodsName, s.shopkeeper_num userNum, s.sk_name userName, g.goods_price goodsPrice, g.goods_add_time goodsAddTime, a.ga_id gaId, a.ga_path mainpic"
				+ " from goods g, goods_album a, shopkeeper_info s"
				+ " where g.goods_num=?0 and a.goods_num=g.goods_num and a.ga_main=1"
				+ " and g.shopkeeper_num=s.shopkeeper_num";
		
		return (SimpleGoods) getSqlQuery(sql, goodsNum)
				.setResultTransformer(Transformers.aliasToBean(SimpleGoods.class))
				.uniqueResult();
	}
}
